/**
 *
 *
 */
package com.fw.timesheet.controller.master;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;

import com.fw.timesheet.common.util.ServiceLocator;
import com.fw.timesheet.controller.common.BaseController;
import com.fw.timesheet.model.Client;
import com.fw.timesheet.model.Project;

/**
 * @author sigit <br>
 * @version 1.0 <br>
 *          Application : timesheet <br>
 *          Copyright (c) fastwire-group 2012 <br>
 * <br>
 */
public class ProjectController extends BaseController {

	private Listbox projectListbox, clientListbox;
	private Textbox name;
	private Textbox country;
	private Project project;
	private Client client;

	@Override
	public void doAfterCompose(Component aWindow) throws Exception {
		// TODO Auto-generated method stub
		super.doAfterCompose(aWindow);
		loadAllProject();
		loadAllClient();
	}

	private void loadAllProject() {
		List<Project> projects = ServiceLocator.getInstance()
				.getProjectService().findAll();
		projectListbox.getItems().clear();

		for (int i = 0; i < projects.size(); i++) {
			project = projects.get(i);

			Listitem item = new Listitem();

			Listcell cell = new Listcell(project.getName());
			cell.setParent(item);

			Client client = ServiceLocator.getInstance().getClientService()
					.findById(project.getClientId());
			cell = new Listcell(client.getName());
			cell.setParent(item);

			cell = new Listcell(project.getStartDate().toString());
			cell.setParent(item);

			cell = new Listcell(project.getStopDate().toString());
			cell.setParent(item);

			item.setParent(projectListbox);
		}

	}

	private void loadAllClient() {
		List<Client> clients = ServiceLocator.getInstance().getClientService()
				.findAll();
		clientListbox.getItems().clear();

		for (int i = 0; i < clients.size(); i++) {
			client = clients.get(i);

			Listitem item = new Listitem();
			item.setValue(client);

			Listcell cell = new Listcell(client.getName());
			cell.setParent(item);

			item.setParent(clientListbox);
		}
	}

}
